#coding=utf-8
from TabPost import *
from TabTalk import *
from tkinter import ttk
from tkinter import *
import _tkinter
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import platform
import getpass
from FiltTalk  import *
from TabMeta  import *
import sys, os, time

if platform.system() == "Windows":
	doc_path = ''
else:
	doc_path = u"/Users/{}/Documents".format(getpass.getuser())
	try:
		os.path.join(doc_path, u'陈意涵')
		os.mkdir(doc_path+u'/陈意涵')
	except Exception as e:
		pass
	doc_path = doc_path+u'/陈意涵/'

class Application():
	"""docstring for Application"""
	def __init__(self):
		self.root = tk.Tk()
		self.root.title("涵仔的Doki工具包")
		self.root.resizable(False, False) ## 禁止改变界面大小


		self.setupMenu()
		# 以下方法用来计算并设置窗体显示时，在屏幕中心居中
		curWidth = 650  # get current width
		curHeight = 700  # get current height
		scnWidth, scnHeight = self.root.maxsize()  # get screen width and height
		tmpcnf = '+%d+%d' % ((scnWidth - curWidth) / 2, (scnHeight - curHeight) / 2)
		self.root.geometry(tmpcnf)

		self.root.after(1000, self.listen)
		self.root.mainloop()

	def auto_shutdown(self):
		# reload(sys)
		# sys.setdefaultencoding("GBK")
		print('10秒后将会关机...')
		time.sleep(5)
		# os.system('dir')
		os.system('shutdown -s -f -t 10')
		exit()

	def write_info(self, text):
		shutdown_info = open(doc_path+'结束信息.txt', 'a+')
		shutdown_info.write(text)
		shutdown_info.write('\n')
		shutdown_info.close()


	def listen(self):
		self.root.after(1000, self.listen)
		if self.tab1.is_timing == True:
			self.tab1.add_time()
		if self.tab2.is_timing == True:
			self.tab2.add_time()
		if self.tab4.is_timing == True:
			self.tab4.add_time()

		try:
			notice = self.tab1.queue.get(False)
		except Exception as e:
			pass
		else:
			if "stop" in notice:
				now_time = int(notice.split(",,")[1])
				now_total = int(notice.split(",,")[2])
				str = ""
				if self.tab1.is_auto_login == False:
					str = "该账号已刷完，请切换账号！！！\n总计用时：{}秒，当前账号用时：{}秒\n总计刷了{}条评论，当前账号刷了{}条评论，剩余有效链接{}条".format(self.tab1.all_time, now_time, self.tab1.all_total, now_total,len(self.tab1.urls))
				else:
					str = "该组{}个账号已刷完\n成功帐号{}个，密码错误帐号{}个，冻结帐号{}个！\n总计用时：{}秒，当前组用时：{}秒\n总计刷了{}条评论，当前组刷了{}条评论，剩余有效链接{}条".format(self.tab1.all_count_account,self.tab1.success_count_account,self.tab1.failed_count_account,self.tab1.frozen_count_account,self.tab1.all_time, now_time, self.tab1.all_total, now_total,len(self.tab1.urls))
				if self.tab1.is_auto_shutdown_var.get() == 1:
					self.write_info(str)
					self.auto_shutdown()
				else:
					tkinter.messagebox.showinfo('温馨提示',str)

			elif notice == "url_miss":
				if len(self.tab1.urls) == 0:
					str = "链接全部刷完，请导入链接"
				else:
					str = "评论的链接数小于{}，请添加链接！！".format(self.tab1.less_url_count)
				if self.tab1.is_auto_shutdown_var.get() == 1:
					self.write_info(str)
					self.auto_shutdown()
				else:
					tkinter.messagebox.showinfo('温馨提示',str)
			elif "error" in notice:
				str = "此软件为意涵专属，请勿盗用！！！\n" + notice.split(",,")[1]
				tkinter.messagebox.showerror("警告",str)

		try:
			notice = self.tab4.queue.get(False)
		except Exception as e:
			pass
		else:
			if "stop" in notice:
				now_time = int(notice.split(",,")[1])
				now_total = int(notice.split(",,")[2])
				str = ""
				if self.tab4.is_auto_login == False:
					str = "该账号已刷完，请切换账号！！！\n总计用时：{}秒，当前账号用时：{}秒\n总计点赞{}条，当前账号点赞{}条，剩余点赞有效链接{}条".format(self.tab4.all_time, now_time, self.tab4.all_total, now_total,len(self.tab4.urls))
				else:
					str = "该组{}个账号已刷完\n成功帐号{}个，密码错误帐号{}个，冻结帐号{}个！\n总计用时：{}秒，当前组用时：{}秒\n总计点赞{}条，当前组点赞{}条，剩余点赞有效链接{}条".format(self.tab4.all_count_account,self.tab4.success_count_account,self.tab4.failed_count_account,self.tab4.frozen_count_account,self.tab4.all_time, now_time, self.tab4.all_total, now_total,len(self.tab4.urls))
				if self.tab4.is_auto_shutdown_var.get() == 1:
					self.write_info(str)
					self.auto_shutdown()
				else:
					tkinter.messagebox.showinfo('温馨提示',str)

			elif notice == "url_miss":
				if len(self.tab1.urls) == 0:
					str = "链接全部点赞完，请导入链接"
				else:
					str = "点赞的链接数小于{}，请添加链接！！".format(self.tab4.less_url_count)
				if self.tab1.is_auto_shutdown_var.get() == 1:
					self.write_info(str)
					self.auto_shutdown()
				else:
					tkinter.messagebox.showinfo('温馨提示',str)
			elif "error" in notice:
				str = "此软件为意涵专属，请勿盗用！！！\n" + notice.split(",,")[1]
				tkinter.messagebox.showerror("警告",str)



		try:
			notice = self.tab2.queue.get(False)
		except Exception as e:
			pass
		else:
			if "stop" in notice:
				now_time = int(notice.split(",,")[1])
				now_total = int(notice.split(",,")[2])
				str = ""
				if self.tab2.is_auto_login == False:
					str = "该账号已发完贴子，请切换账号！！！\n总计用时：{}秒，当前账号用时：{}秒\n总计发了{}条帖子，当前账号发了{}条帖子".format(self.tab2.all_time, now_time, self.tab2.all_total, now_total)
				else:
					str = "该组{}个账号已发完\n成功帐号{}个，密码错误帐号{}个，冻结帐号{}个！\n总计用时：{}秒，当前组用时：{}秒\n总计发了{}条帖子，当前组发了{}条帖子".format(self.tab2.all_count_account,self.tab2.success_count_account,self.tab2.failed_count_account,self.tab2.frozen_count_account,self.tab2.all_time, now_time, self.tab2.all_total, now_total)
				if self.tab2.is_auto_shutdown_var.get() == 1:
					self.write_info(str)
					self.auto_shutdown()
				else:
					tkinter.messagebox.showinfo('温馨提示',str)

		try:
			notice = self.tab3.queue.get(False)
		except Exception as e:
			pass
		else:
			if "stop" in notice:
				str = "当前{}链接已经筛选完成，有效链接数为：{}个".format(self.tab3.all_total,self.tab3.valid_total)
				tkinter.messagebox.showinfo('温馨提示',str)


	def setupMenu(self):
		#创建菜单栏
		menu = Menu(self.root)

		#创建二级菜单
		talk_menu = Menu(menu,tearoff=0)
		# talk_menu.add_command(label="选择链接文本",command=self.tab1_open_link)
		# talk_menu.add_command(label="选择评论话术",command=self.tab1_open_talk)

		send_menu = Menu(menu,tearoff=0)
		# send_menu.add_command(label="选择链接文本",command=self.tab2_open_link)
		# send_menu.add_command(label="选择评论话术",command=self.tab2_open_talk)

		about_menu = Menu(menu,tearoff=0)
		about_menu.add_command(label="为了涵涵秃头！！！")
		author_menu = Menu(menu,tearoff=0)
		author_menu.add_command(label="@AskeyNil：完成基本功能，GUI界面搭建")
		author_menu.add_command(label="@一棵木鱼：完成自动登录部分验证码识别")

		#在菜单栏中添加以下一级菜单
		# menu.add_cascade(label="评论",menu=talk_menu)
		# menu.add_cascade(label="发帖",menu=send_menu)
		menu.add_cascade(label="作者微博：@AskeyNil,@一棵木鱼",menu=author_menu)
		menu.add_cascade(label="关于",menu=about_menu)


		self.root['menu'] = menu

		## 建立三个tab页
		tabControl = ttk.Notebook(self.root)
		tab1 = ttk.Frame(tabControl)
		tabControl.add(tab1, text='自动登录评论')
		tabControl.pack(expand=1, fill="both")
		tab2 = Frame(tabControl)
		tabControl.add(tab2, text='自动登录发帖')
		tab4 = Frame(tabControl)
		tabControl.add(tab4, text='自动登录点赞')
		tab3 = Frame(tabControl)
		tabControl.add(tab3, text='判断链接')
		self.tab1 = TabTalk(tab1)
		self.tab2 = TabPost(tab2)
		self.tab3 = FiltTalk(tab3)
		self.tab4 = TabMeta(tab4)
		# TabBase(tab1)
		# self.setup_talk_UI(tab1)
		# self.setup_send_UI(tab2)
		# self.setup_link_UI(tab3)

if __name__ == "__main__":
	Application()
